﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.ServiceModel.Syndication;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace C9RSSPagingExample
{
    class Program
    {
        static void Main(string[] args)
        {
            string areaBaseUrl = "http://s.ch9.ms/Events/TechEd/NorthAmerica/2014/";
            int page = 1;

            if (!areaBaseUrl.EndsWith("/"))
                areaBaseUrl += "/";

            bool getpage = true;

            while (getpage)
            {

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(areaBaseUrl + "rss?page=" + page);

                // Set some reasonable limits on resources used by this request
                request.MaximumAutomaticRedirections = 4;
                request.MaximumResponseHeadersLength = 4;

                //if (userid.Length > 0 && password.Length > 0)
                //    request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(userid + ":" + password)));

                HttpWebResponse response;

                try
                {
                    response = (HttpWebResponse)request.GetResponse();
                }
                catch (Exception)
                {

                    throw;
                }

                if (request.RequestUri.ToString().ToLowerInvariant() != request.Address.ToString().ToLowerInvariant() )
                {
                    getpage = false;
                    continue;
                }

                // Get the stream associated with the response.
                Stream receiveStream = response.GetResponseStream();

                // Pipes the stream to a higher level stream reader with the required encoding format. 
                StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);
                XmlReader reader = XmlReader.Create(readStream);
                SyndicationFeed feed = SyndicationFeed.Load(reader);


                foreach (var f in feed.Items)
                {
                    Console.WriteLine(f.Title.Text);
                }

                page += 1;

                response.Close();
                readStream.Close();
            }
        }
    }
}
